
export const getBaseUrl = () => {
    let url
    const { PROD, VITE_API_baseURL } = import.meta.env
    if (PROD) {
        url = `${window.location.origin}${window.location.pathname.slice(
            0,
            window.location.pathname.indexOf('/index.html')
        )}/prodapi`
    } else {
        url = `${window.location.origin}${VITE_API_baseURL}`
    }
    return url
}

export const getImgSrc = path => {
    const _path = `../assets/${path}`
    const modules = import.meta.glob('../assets/**/*.png', { eager: true })
    return modules[_path].default
}

/**
 * 扁平化树转树型结构
 * @param {{unknown[]}} list
 * @param {{
 * idKey: string,
 * parentIdKey: string,
 * fullResult?: boolean
 * }} options
 * @returns
 */
export const arrToTree = (list, options = {}) => {
    const { idKey = 'id', parentIdKey = 'parentId', fullResult = false } = options
    let map = {}
    let result = []

    if (!Array.isArray(list)) {
        return []
    }

    list.forEach(item => {
        map[item[idKey]] = item
    })

    list.forEach(item => {
        let parent = map[item[parentIdKey]]
        if (parent) {
            (parent.children || (parent.children = [])).push(item)
        } else {
            result.push(item)
        }
    })

    if (fullResult) {
        return {
            list,
            map,
            tree: result
        }
    }

    return result
}

// 树形数据扁平化
export const treeFlat = node => {
    let queue = [node]
    let data = [] // 返回的数组结构
    while (queue.length !== 0) { // 当队列为空时就跳出循环
        let item = queue.shift() // 取出队列中第一个元素
        data.push({
            id: item.id,
            parentId: item.parentId,
            name: item.name
        })
        let children = item.children // 取出该节点的孩子
        if (children) {
            for (let i = 0; i < children.length; i++) {
                queue.push(children[i]) // 将子节点加入到队列尾部
            }
        }
    }
}

// 过滤树
export function filterTree (tree = [], options = {}) {
    const { childrenKey = 'children', filterFn = () => true } = options
    const result = []

    tree.forEach(node => {
        if (filterFn(node)) {
            const newNode = { ...node }
            if (Array.isArray(newNode[childrenKey]) && newNode[childrenKey].length) {
                newNode[childrenKey] = filterTree(newNode[childrenKey], options)
            }
            result.push(newNode)
        }
    })

    return result
}

export const getType = (val, lowerCase = true) => {
    const type = Object.prototype.toString.call(val).slice(8, -1)
    return lowerCase ? type.toLowerCase() : type
}

export const isAsyncFunction = val => {
    return getType(val, false) === 'AsyncFunction'
}

export const getStartEndTime = (val = []) => {
    if (!Array.isArray(val)) val = []
    const [start = '', end = ''] = val
    return {
        start,
        end
    }
}

export const buildOptionsMap = (opts = {}) => {
    const { list = [], labelField = 'label', valueField = 'value' } = opts
    const map = {}

    list.forEach(item => {
        const label = item[labelField]
        const value = item[valueField]
        const result = {
            ...item,
            label,
            value
        }
        map[value] = result
    })

    return map
}

// 递归查找第一个符合条件的内容
export const findFirstByCondition = (treeList = [], condition = () => false) => {
    for (let i = 0; i < treeList.length; i++) {
        const item = treeList[i]

        if (condition(item)) {
            return item
        }

        if (Array.isArray(item.children) && item.children.length) {
            const res = findFirstByCondition(item.children, condition)
            if (res) {
                return res
            }
        }
    }
}